package com.zhiche.lisa.tms.controller;

import com.alibaba.fastjson.JSON;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.oms.dto.ShipmentOrderDTO;
import com.zhiche.lisa.tms.dao.model.ShipOrder;
import com.zhiche.lisa.tms.dao.model.bo.GenerateShipOrderBO;
import com.zhiche.lisa.tms.dto.OrderAssignDTO;
import com.zhiche.lisa.tms.dto.ShipmentAutoDTO;
import com.zhiche.lisa.tms.service.IShipOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 运单 接口
 *
 * @author lbl
 * @since 2018-06-19
 */
@RestController
@RequestMapping(value = "/shipOrder")
@Api(value = "/shipOrder", description = "运单接口", tags = {"运单接口"})
public class ShipOrderController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ShipOrderController.class);

    @Autowired
    private IShipOrderService shipOrderService;

    @Value("${security.jwt.header}")
    private String header;

    @PostMapping(value = "/generateShipOrder")
    @ApiOperation(value = "生成运单", notes = "生成运单",
            produces = MediaType.APPLICATION_JSON_VALUE,
            response = RestfulResponse.class)
    public RestfulResponse generateShipOrder(@RequestBody GenerateShipOrderBO generateShipOrderBO,
                                             HttpServletRequest request) {
        LOGGER.info("ShipOrderController.generateShipOrder params:{} ", JSON.toJSONString(generateShipOrderBO));
        RestfulResponse<Object> result = null;

        try {
            String authHeader = request.getHeader(header);
            shipOrderService.generateShipOrder(generateShipOrderBO, authHeader);
            result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.ERROR.getText(), null);
        } catch (BaseException e) {
            LOGGER.error("ShipOrderController.generateShipOrder BaseException... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("ShipOrderController.generateShipOrder ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }

        return result;
    }

    @GetMapping(value = "/getShipOrderByItemId")
    @ApiOperation(value = "根据订单 id 获取运单信息",
            notes = "根据订单 id 获取运单信息",
            produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<ShipOrder> getShipOrderByItemId(Long itemId) {

        RestfulResponse<ShipOrder> result = null;

        try {
            result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), shipOrderService.getShipOrderByItemId(itemId));
        } catch (BaseException e) {
            LOGGER.error("ShipOrderController.getShipOrderByItemId BaseException... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("ShipOrderController.getShipOrderByItemId ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }

        return result;
    }

    @PostMapping(value = "/autoShipment")
    @ApiOperation(value = "自动生成装车单", notes = "自动生成装车单",
            produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<List<OrderAssignDTO>> autoShipment
            (@RequestBody ShipmentAutoDTO shipmentAutoDTO, HttpServletRequest request) {
        LOGGER.info("ShipOrderController.autoShipment params:{} ", JSON.toJSONString(shipmentAutoDTO));
        RestfulResponse<List<OrderAssignDTO>> result = null;
        String authHeader = request.getHeader(header);
        try {
            List<OrderAssignDTO> orderAssignDTOList = shipOrderService.autoShipment(shipmentAutoDTO, authHeader);
            result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), orderAssignDTOList);
        } catch (BaseException e) {
            LOGGER.error("ShipOrderController.autoShipment BaseException... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("ShipOrderController.autoShipment ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/addShipment")
    @ApiOperation(value = "根据oms的数据自动生成装车单",
            notes = "根据oms的数据自动生成装车单",
            produces = MediaType.APPLICATION_JSON_VALUE,
            response = RestfulResponse.class)
    public RestfulResponse<List<OrderAssignDTO>> addShipment(@RequestBody ShipmentOrderDTO shipmentOrderDTO,
                                                             HttpServletRequest request) {
        LOGGER.info("ShipOrderController.addShipment param {}", JSON.toJSONString(shipmentOrderDTO));
        RestfulResponse<List<OrderAssignDTO>> result = null;
        String authHeader = request.getHeader(header);
        try {
            List<OrderAssignDTO> orderAssignDTOList = shipOrderService.addShipment(shipmentOrderDTO, authHeader);
            result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), orderAssignDTOList);
        } catch (BaseException e) {
            LOGGER.error("ShipOrderController.addShipment BaseException... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("ShipOrderController.addShipment ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/getShipOrderItemCount")
    @ApiOperation(value = "获取指令下商品车数量", notes = "获取指令下商品车数量", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = String.class), @ApiResponse(code = -1, message = "操作失败", response = Object.class)})
    public RestfulResponse getShipOrderItemCount(@RequestBody List<Map<String,Object>> shipSourceKeys) {
        LOGGER.info("ShipOrderController.getShipOrderItemCount（获取指令下商品车数量） params : {} ", shipSourceKeys);
        RestfulResponse<List<Map<String,Object>>> result = new RestfulResponse<>(0, "success", null);
        try {
            List<Map<String,Object>> list = shipOrderService.queryShipOrderItemCount(shipSourceKeys);
            result.setData(list);
        } catch (BaseException e) {
            LOGGER.error("ShipOrderController.getShipOrderItemCount ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("ShipOrderController.getShipOrderItemCount ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }
}
